package com.remind;

import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.remind.activity.MessageActivity;

public class MessageBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "MessageReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "有新消息咯");
        //管理锁屏的一个服务
        KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        String text = km.inKeyguardRestrictedInputMode() ? "锁屏了" : "屏幕亮着的";
        Log.i(TAG, "text: " + text);
        if (km.inKeyguardRestrictedInputMode()) {
            Log.i(TAG, "onReceive:锁屏了 ");
            //判断是否锁屏
            Intent alarmIntent = new Intent(context, MessageActivity.class);
            //在广播中启动Activity的context可能不是Activity对象，所以需要添加NEW_TASK的标志，否则启动时可能会报错。
            alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP) ;
            context.startActivity(alarmIntent); //启动显示锁屏消息的activity
        }
    }


}
